/*
 * Copyright (c) 2011 Nathan Byrd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.stackconfig.web.gwt.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import org.stackconfig.web.gwt.client.activities.StackConfigActivityMapper;
import org.stackconfig.web.gwt.client.widgets.AboutPopupPanel;
import org.stackconfig.web.gwt.client.widgets.SimpleLayoutPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class StackConfigMain implements EntryPoint {

    interface StackConfigMainUiBinder extends
            UiBinder<DockLayoutPanel, StackConfigMain> {
    }


    private static StackConfigMainUiBinder uiBinder = GWT.create(StackConfigMainUiBinder.class);


    private final StackConfigGInjector injector = GWT.create(StackConfigGInjector.class);

    @UiField
    SimpleLayoutPanel appPanel;

    @UiField
    Anchor aboutLink;

    ActivityManager activityManager;


    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        EventBus eventBus = injector.getEventBus();
        PlaceController placeController = injector.getPlaceController();
        DockLayoutPanel root = uiBinder.createAndBindUi(this);

        ActivityMapper activityMapper = new StackConfigActivityMapper();
        activityManager = new ActivityManager(activityMapper, eventBus);
        activityManager.setDisplay(appPanel);

        PlaceHistoryMapper historyMapper = injector.getHistoryMapper();
        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        Place defaultPlace = injector.getPlaceFactory().getDesignPlace();
        historyHandler.register(placeController, eventBus, defaultPlace);


        RootLayoutPanel.get().add(root);
        historyHandler.handleCurrentHistory();


    }

    @UiHandler("aboutLink")
    public void onClickAboutLink(ClickEvent clickEvent) {
        AboutPopupPanel popup = new AboutPopupPanel();
        popup.setGlassEnabled(true);
        popup.setAnimationEnabled(true);
        popup.setStyleName("aboutPopup");
        popup.setSize("100%", "100%");

        popup.setModal(true);
        popup.center();
        popup.show();
    }

}
